var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = e("BaseModuleUI"),
    a = e("NotifyEventType"),
    i = e("GlobalParams"),
    r = e("AudioManager"),
    s = e("PureHelper"),
    l = e("Game"),
    c = e("node_team"),
    d = e("role_node"),
    h = e("team_node_info"),
    _ = cc._decorator,
    u = _.ccclass,
    p = _.property,
    m = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.tower_team = l.TeamInfo.left_team),
                (t.tower_id = 0),
                (t.tower_ske = null),
                (t.hp_bar = null),
                (t.tower_map_node = null),
                (t.tower_boom = null),
                (t.role_solider = null),
                (t.attack_target = null),
                (t.tower_attack = !1),
                (t.tower_info = null),
                (t.tower_hp = 0),
                (t.tower_is_destroy = !1),
                t
            );
        }
        return (
            __extends(t, e),
            (t.prototype.initView = function () {
                (this.tower_is_destroy = !1),
                    (this.tower_attack = !1),
                    this.getComponent(c.default).setTeamInfo(this.tower_team),
                    (this.role_solider = this.node.parent),
                    (this.hp_bar.width = 180),
                    (this.hp_bar.color =
                        this.tower_team ==
                        (i.GlobalParams.MatchMyIndex % 2 == 0 ? l.TeamInfo.left_team : l.TeamInfo.right_team)
                            ? new cc.Color(94, 189, 255)
                            : new cc.Color(250, 20, 20)),
                    (this.tower_info = l.default.tower_info[this.tower_id]),
                    (this.tower_hp = this.tower_info.hp),
                    this.tower_ske.setAnimation(0, "idle", !0),
                    this.tower_boom && (this.tower_boom.node.active = !1),
                    this.showTowerScope(!1),
                    this.updateTime();
            }),
            (t.prototype.updateTime = function () {
                var e = this;
                if (
                    i.GlobalParams.GameStart &&
                    (this.attack_target && cc.isValid(this.attack_target, !0)
                        ? !this.tower_attack && cc.isValid(this.attack_target, !0) && this.towerAttackTarget()
                        : this.getAttackTarget(),
                    !this.tower_is_destroy)
                ) {
                    var t = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    if (t && t.getComponent(c.default).getTeamInfo() != this.tower_team) {
                        var o = Math.abs(s.default.Distance(this.node.getPosition(), t.getPosition()));
                        this.showTowerScope(o < 500);
                    } else this.showTowerScope(!1);
                }
                this.scheduleOnce(function () {
                    e.updateTime();
                }, i.GlobalParams.GameUpdateTime);
            }),
            (t.prototype.getAttackTarget = function () {
                for (var e = 9999, t = null, o = 0, n = this.role_solider.children; o < n.length; o++) {
                    var a = n[o],
                        i = a.getComponent(c.default).getTeamInfo();
                    if (a.isValid && i != this.tower_team && i != l.TeamInfo.rest_team) {
                        var r = Math.abs(s.default.Distance(this.node.getPosition(), a.getPosition()));
                        if (r > 450) continue;
                        r < e && ((e = r), (t = a));
                    }
                }
                t && (this.attack_target = t);
            }),
            (t.prototype.towerAttackTarget = function () {
                var e = this;
                if (
                    !this.tower_is_destroy &&
                    ("role" == this.attack_target.group || "soldier" == this.attack_target.group)
                ) {
                    (this.tower_attack = !0), this.tower_ske.setAnimation(0, "attack", !1);
                    var t = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    cc.isValid(t, !0) &&
                        Math.abs(s.default.Distance(this.node.getPosition(), t.getPosition())) < 700 &&
                        r.default.getInstance().playFx("tower_attack"),
                        this.tower_ske.setCompleteListener(function () {
                            e.tower_ske.setCompleteListener(null),
                                e.tower_ske.setAnimation(0, "idle", !0),
                                e.attack_target &&
                                    cc.isValid(e.attack_target, !0) &&
                                    e.attack_target.active &&
                                    (Math.abs(s.default.Distance(e.node.getPosition(), e.attack_target.getPosition())) >
                                    450
                                        ? (e.attack_target = null)
                                        : ("role" == e.attack_target.group &&
                                              e.attack_target
                                                  .getComponent(d.default)
                                                  .roleBeHit(e.tower_info.attack, 10),
                                          "soldier" == e.attack_target.group &&
                                              e.attack_target
                                                  .getComponent(h.default)
                                                  .soldierBeHit(e.tower_info.attack, 10),
                                          e.emitEvent(a.default.SEND_TOWER_LIGHTING_TARGET_INFO, e.attack_target))),
                                cc.isValid(e.attack_target, !0) && !e.attack_target.active && (e.attack_target = null),
                                (e.tower_attack = !1);
                        });
                }
            }),
            (t.prototype.towerBeHit = function (e, t) {
                var o = this;
                if (
                    i.GlobalParams.GameDestroyTower[this.tower_team == l.TeamInfo.left_team ? 0 : 1] == this.tower_id &&
                    !this.tower_is_destroy &&
                    ("idle" == this.tower_ske.animation &&
                        (this.tower_ske.setAnimation(0, "hurt", !1),
                        this.tower_ske.setCompleteListener(function () {
                            o.tower_ske.setCompleteListener(null), o.tower_ske.setAnimation(0, "idle", !0);
                        })),
                    (this.tower_hp -= e),
                    (this.tower_hp = this.tower_hp < 0 ? 0 : this.tower_hp),
                    (this.hp_bar.width = (180 * this.tower_hp) / this.tower_info.hp),
                    this.emitEvent(a.default.SEND_SHOW_BE_HIT_INFO, {
                        attack: e,
                        my_team: this.tower_team == l.TeamInfo.left_team ? 0 : 1,
                        end_pos: cc.v2(this.node.getPosition().x, this.node.getPosition().y + 80)
                    }),
                    this.tower_hp <= 0 && !this.tower_is_destroy)
                ) {
                    (this.tower_is_destroy = !0),
                        i.GlobalParams.GameDestroyTower[this.tower_team == l.TeamInfo.left_team ? 0 : 1]++,
                        this.emitEvent(a.default.SEND_ROLE_BE_KILL_INFO, {
                            kill: t,
                            be_kill: l.KillType.tower,
                            team: this.tower_team
                        }),
                        this.tower_ske.setAnimation(0, "death", !1),
                        this.tower_boom &&
                            ((this.tower_boom.node.active = !0), this.tower_boom.setAnimation(0, "bz", !1));
                    var n = this.role_solider.getChildByName("role_team_" + i.GlobalParams.MatchMyIndex);
                    cc.isValid(n, !0) &&
                        Math.abs(s.default.Distance(this.node.getPosition(), n.getPosition())) < 700 &&
                        r.default.getInstance().playFx("tower_die"),
                        this.showTowerScope(!1),
                        this.tower_ske.setCompleteListener(function () {
                            o.tower_ske.setCompleteListener(null),
                                2 == o.tower_id
                                    ? ((i.GlobalParams.GameStart = !1),
                                      (i.GlobalParams.GamePlayerWin =
                                          o.tower_team !=
                                          (i.GlobalParams.MatchMyIndex % 2 == 0
                                              ? l.TeamInfo.left_team
                                              : l.TeamInfo.right_team)),
                                      o.emitEvent(a.default.SEND_GAME_OVER_INFO),
                                      o.node.destroy(),
                                      cc.log("游戏结束"))
                                    : o.node.destroy();
                        });
                }
            }),
            (t.prototype.showTowerScope = function (e) {
                this.tower_map_node.getChildByName(this.node.name).active = e;
            }),
            __decorate([p({type: cc.Enum(l.TeamInfo), displayName: "防御塔阵容"})], t.prototype, "tower_team", void 0),
            __decorate([p({type: cc.Float, displayName: "防御塔ID"})], t.prototype, "tower_id", void 0),
            __decorate([p(sp.Skeleton)], t.prototype, "tower_ske", void 0),
            __decorate([p(cc.Node)], t.prototype, "hp_bar", void 0),
            __decorate([p(cc.Node)], t.prototype, "tower_map_node", void 0),
            __decorate([p(sp.Skeleton)], t.prototype, "tower_boom", void 0),
            __decorate([u], t)
        );
    })(n.default);
o.default = m;
